package me.cxlr.qinlauncher2.view.launcher;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentActivity;

import me.cxlr.qinlauncher2.R;
import me.cxlr.qinlauncher2.constants.Constants;
import me.cxlr.qinlauncher2.dao.DockActionDao;
import me.cxlr.qinlauncher2.manager.ActionManager;
import me.cxlr.qinlauncher2.manager.IconManager;
import me.cxlr.qinlauncher2.manager.SoftwareManager;
import me.cxlr.qinlauncher2.model.DockAction;
import me.cxlr.qinlauncher2.util.DisplayUtil;
import me.cxlr.qinlauncher2.util.SharedPreferencesUtil;

/**
 * @author 酷安用户:山轮纸
 */
public class Dock {
    private volatile static Dock dock;

    private Dock() {
        dockActionDao = new DockActionDao();
    }

    public static Dock getInstance() {
        if (dock == null) {
            synchronized (Dock.class) {
                if (dock == null) {
                    dock = new Dock();
                }
            }
        }
        return dock;
    }

    private final DockActionDao dockActionDao;

    /**
     * 加载Dock
     *
     * @param llDock1  图标1
     * @param llDock2  图标2
     * @param llDock3  图标3
     * @param llDock4  图标4
     * @param llDock5  图标5
     * @param imgDock1 图标1
     * @param imgDock2 图标2
     * @param imgDock3 图标3
     * @param imgDock4 图标4
     * @param imgDock5 图标5
     * @param cvDock   dock
     * @param activity activity
     * @param context  context
     */
    public void loadDock(LinearLayoutCompat llDock1, LinearLayoutCompat llDock2, LinearLayoutCompat llDock3, LinearLayoutCompat llDock4, LinearLayoutCompat llDock5,
                         LinearLayoutCompat llDockIcon1, LinearLayoutCompat llDockIcon2, LinearLayoutCompat llDockIcon3, LinearLayoutCompat llDockIcon4, LinearLayoutCompat llDockIcon5,
                         AppCompatImageView imgDock1, AppCompatImageView imgDock2, AppCompatImageView imgDock3, AppCompatImageView imgDock4, AppCompatImageView imgDock5,
                         CardView cvDock, FragmentActivity activity, Context context) {
        boolean useDockFlag = SharedPreferencesUtil.getInstance().getBoolean("use_dock", true);
        float dockIconSize = Float.parseFloat(SharedPreferencesUtil.getInstance().getString("dock_icon_size", "56"));
        int iconNumber = Integer.parseInt(SharedPreferencesUtil.getInstance().getString("dock_icons", "5"));

        if (useDockFlag) {
            this.setIconSize(context, imgDock1, dockIconSize);
            this.setIconSize(context, imgDock2, dockIconSize);
            this.setIconSize(context, imgDock3, dockIconSize);
            this.setIconSize(context, imgDock4, dockIconSize);
            this.setIconSize(context, imgDock5, dockIconSize);

            switch (iconNumber) {
                case Constants.DOCK_ICON_NUMBER_3:
                    llDockIcon1.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon1"));
                    setIcon(context, iconNumber, "icon1", imgDock1, llDockIcon1);
                    llDockIcon2.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon2"));
                    setIcon(context, iconNumber, "icon2", imgDock2, llDockIcon2);
                    llDockIcon3.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon3"));
                    setIcon(context, iconNumber, "icon3", imgDock3, llDockIcon3);
                    llDock4.setVisibility(View.GONE);
                    llDock5.setVisibility(View.GONE);
                    break;
                case Constants.DOCK_ICON_NUMBER_5:
                    llDockIcon1.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon1"));
                    setIcon(context, iconNumber, "icon1", imgDock1, llDockIcon1);
                    llDockIcon2.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon2"));
                    setIcon(context, iconNumber, "icon2", imgDock2, llDockIcon2);
                    llDockIcon3.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon3"));
                    setIcon(context, iconNumber, "icon3", imgDock3, llDockIcon3);
                    llDock4.setVisibility(View.VISIBLE);
                    llDockIcon4.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon4"));
                    setIcon(context, iconNumber, "icon4", imgDock4, llDockIcon4);
                    llDock5.setVisibility(View.VISIBLE);
                    llDockIcon5.setOnClickListener(view -> this.openDockAction(activity, context, iconNumber, "icon5"));
                    setIcon(context, iconNumber, "icon5", imgDock5, llDockIcon5);
                    break;
                default:
                    break;
            }
        } else {
            cvDock.setVisibility(View.GONE);
        }
    }

    /**
     * 设置图标的尺寸
     *
     * @param context   上下文
     * @param imageView 图标
     * @param iconSize  图标大小
     */
    private void setIconSize(Context context, AppCompatImageView imageView, float iconSize) {
        ViewGroup.LayoutParams params = imageView.getLayoutParams();
        params.height = DisplayUtil.dpToPx(context, iconSize);
        params.width = DisplayUtil.dpToPx(context, iconSize);
        imageView.setLayoutParams(params);
    }

    /**
     * 点击后打开应用或应用抽屉
     *
     * @param activity   activity
     * @param context    context
     * @param iconNumber 图标数量 3或5
     * @param iconName   第几个图标
     */
    private void openDockAction(FragmentActivity activity, Context context, int iconNumber, String iconName) {
        DockAction dockAction = dockActionDao.findDockActionByIconNumberAndIconName(iconNumber, iconName);
        switch (dockAction.getActionType()) {
            case Constants.DOCK_IS_APP:
                SoftwareManager.getInstance().openApplication(activity, context, dockAction.getApp().getPackageName(), dockAction.getApp().getClazzName());
                break;
            case Constants.DOCK_IS_APP_DRAWER:
                ActionManager.getInstance().openAction(activity, context, dockAction.getAction().getActionName());
                break;
            default:
                break;
        }
    }

    /**
     * 设置图标
     *
     * @param context    上下文
     * @param iconNumber 图标数量 3或5
     * @param iconName   第几个图标
     * @param imageView  图标imageView
     */
    private void setIcon(Context context, int iconNumber, String iconName,
                         AppCompatImageView imageView, LinearLayoutCompat linearLayout) {
        DockAction dockAction = dockActionDao.findDockActionByIconNumberAndIconName(iconNumber, iconName);
        switch (dockAction.getActionType()) {
            case Constants.DOCK_IS_APP:
                Drawable icon = IconManager.getInstance().getApplicationIcon(dockAction.getApp().getPackageName(), dockAction.getApp().getClazzName());
                if (icon != null) {
                    imageView.setImageDrawable(icon);
                    linearLayout.setFocusable(true);
                }
                break;
            case Constants.DOCK_IS_APP_DRAWER:
                imageView.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.icon_application));
                linearLayout.setFocusable(true);
                break;
            default:
                imageView.setImageDrawable(null);
                linearLayout.setFocusable(false);
                break;
        }
    }
}
